package com.coocaa.ops.admin.v1.cornericontype.service;

import com.coocaa.ops.admin.base.language.CommonLanguageProperties;
import com.coocaa.ops.admin.v1.cornericontype.biz.CornerIconTypeBizImpl;
import com.coocaa.ops.admin.v1.cornericontype.model.GlobalCornerIconTypeModel;
import com.coocaa.ops.common.core.base.model.PageResultModel;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.core.base.util.PageQueryUtil;
import com.coocaa.ops.common.core.biz.auth.entity.SysUserEntity;
import com.coocaa.ops.common.core.biz.cornericontype.entity.GlobalCornerIconTypeEntity;
import com.coocaa.ops.common.tool.base.InstanceUtil;
import com.coocaa.ops.common.tool.exception.BusinessException;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bijiahao
 * @date : 2019/3/22.
 * @description
 */
@Service
public class CornerIconTypeServiceImpl implements ICornerIconTypeService {
    @Autowired
    private CornerIconTypeBizImpl cornerIconTypeBiz;
    private static CommonLanguageProperties commonLanguageProperties;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(GlobalCornerIconTypeEntity entity, SysUserEntity user) {
        Integer userId = user.getId();

        if (entity.getId() == null) {
            //新增
            checkDuplicateTypeName(entity, true);
            entity.setFlag(1);
            entity.setCreator(userId);
            entity.setModifier(userId);
            cornerIconTypeBiz.add(entity);
        } else {
            //更新
            checkDuplicateTypeName(entity, false);
            entity.setModifier(userId);
            cornerIconTypeBiz.updateById(entity);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updatePriority(Integer typeId, Integer priority, Integer userId) {
        GlobalCornerIconTypeEntity update = new GlobalCornerIconTypeEntity();
        update.setId(typeId);
        update.setModifier(userId);
        cornerIconTypeBiz.updateById(update);
    }

    @Override
    public GlobalCornerIconTypeEntity getDetailInfo(Integer id) {
        return cornerIconTypeBiz.queryById(id);
    }

    @Override
    public Map<String, Object> getPageResult(GlobalCornerIconTypeModel model) {
        Map<String, Object> params = PageQueryUtil.getFilterMap(model);
        PageResultModel<GlobalCornerIconTypeEntity> pageResult = cornerIconTypeBiz.getCommonPageList(params, false);
        return R.ok(pageResult);
    }

    @Override
    public Map deleteBatch(Integer[] ids, SysUserEntity user) {
        String result = this.deleteBatch(ids, user.getId());
        if (R.SUCCESS.equals(result)) {
            return R.ok().msg(commonLanguageProperties.getDeleteSuccess());
        } else {
            return R.fail(result);
        }
    }

    @Autowired
    public void setCommonLanguageProperties(CommonLanguageProperties commonLanguageProperties) {
        CornerIconTypeServiceImpl.commonLanguageProperties = commonLanguageProperties;
    }

    @Override
    public Map<String, Object> getCornerTypes() {
        Map<String, Object> params = InstanceUtil.newHashMap(2);
        params.put("flag", 1);
        List<GlobalCornerIconTypeEntity> list = cornerIconTypeBiz.getMapper().queryCommonPageList(params);
        return R.ok(list);
    }

    /*************** ===私有方法=== ********************* ===start=== ***************************************/
    /**
     * 处理批量删除
     *
     * @param ids
     * @param userId
     * @return
     */
    private String deleteBatch(Integer[] ids, Integer userId) {
        if (ArrayUtils.isNotEmpty(ids)) {
            for (Integer id : ids) {
                GlobalCornerIconTypeEntity typeEntity = cornerIconTypeBiz.queryById(id);
                //校验是否存在已经绑定了该分类的角标
                if (cornerIconTypeBiz.isIconTypeHasUsed(id)) {
                    return "[ID：" + id + "][" + commonLanguageProperties.getName() + typeEntity.getTypeName() + "]" + commonLanguageProperties.getInUse() + commonLanguageProperties.getNotDelete();
                }
            }
            //批量 逻辑删除
            cornerIconTypeBiz.logicDelBatchByIds(ids);
        }
        return R.SUCCESS;
    }

    /**
     * 校验是否存在重复的类型名
     *
     * @param newEntity
     * @param isAdding  是否新增
     */
    private void checkDuplicateTypeName(GlobalCornerIconTypeEntity newEntity, boolean isAdding) throws BusinessException {
        GlobalCornerIconTypeEntity entity = cornerIconTypeBiz.queryByTypeName(newEntity.getTypeName());
        if (entity != null) {
            //如果是新增，则不为空标识一定有重复
            //如果不是新增，还需要看是不是该类型
            if (isAdding || !entity.getId().equals(newEntity.getId())) {
                Map<String, Object> errMap = new HashMap<>();
                errMap.put("typeName", entity.getTypeName());
                throw new BusinessException(errMap, commonLanguageProperties.getExistData());
            }
        }
    }

}
